<?php

namespace addons\travel\controller;

use addons\travel\controller\Base;
use think\Db;
use think\Config as FaConfig;
use fast\Random;
use think\exception\HttpResponseException;
use addons\travel\model\Goods as GoodsModel;
use addons\travel\model\Service as ServiceModel;

class Goods extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品列表
     * @return void
     */
    public function index()
    {
        $per_page=$this->request->request('per_page','10');
        $is_recommend=$this->request->request('is_recommend',1);
        $type=$this->request->request('type');
        $pid=$this->request->request('pid',0);
        $car_seat=$this->request->request('car_seat');
        $trip_day=$this->request->request('trip_day');
        $keyword = $this->request->request('keyword');
        if($keyword){
            $map['title']=['like','%'.$keyword.'%'];
        }
        if($is_recommend==2){
            $map['state']=2;//2为推荐
        }else{
            $map['state']=['in',[1,2]];//[1,2]为正常或推荐
        }
        if(!$type){
            $this->error('参数错误.type');
        }
        $map['type']=$type;
        $map['pid']=$pid;
        if($car_seat){
            $map['car_seat']=$car_seat;
        }
        if($trip_day){
            $map['trip_day']=$trip_day;
        }

        $records=GoodsModel::where($map)->order('weigh desc')->paginate($per_page);
        $records->each(function ($record){
            //处理标签
            $service_records=[];
            $service_ids=$record['service_ids'];
            unset($record['service_ids']);
            if($service_ids){
                $service_records=ServiceModel::where('id','in',$service_ids)->select();
                foreach ($service_records as &$service_record){
                    $service_lang_record=$this->doLangData($service_record['lang_data']);
                    //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
                    if(!empty($service_lang_record)){
                        foreach ($service_lang_record as $slk=>$slv){
                            $service_record[$slk]=$slv;
                        }
                    }
                    unset($service_record['lang_data']);
                }
            }
            $record['service']=$service_records;
            //处理协议-列表里面不处理协议-详情再处理-和前端商量好再调一次协议接口。详情里也就不用处理了
            $lang_record=$this->doLangData($record['lang_data']);
            //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
            if(!empty($lang_record)){
                foreach ($lang_record as $lk=>$lv){
                    $record[$lk]=$lv;
                }
            }
            unset($record['lang_data']);
        });
        $this->success('操作成功',$records);
    }
    public function detail()
    {
        $id = $this->request->get('id');
        if(!$id){
            $this->error('参数错误.id');
        }
        $lang_slug=$this->langSlug;

        $record=GoodsModel::detail(['id'=>$id,'lang_slug'=>$lang_slug]);
//        $record=GoodsModel::where('id',$id)->find();
//        //处理标签
//        $service_records=[];
//        $service_ids=$record['service_ids'];
//        unset($record['service_ids']);
//        if($service_ids){
//            $service_records=ServiceModel::where('id','in',$service_ids)->select();
//            foreach ($service_records as &$service_record){
//                $service_lang_record=$this->doLangData($service_record['lang_data']);
//                //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
//                if(!empty($service_lang_record)){
//                    foreach ($service_lang_record as $slk=>$slv){
//                        $service_record[$slk]=$slv;
//                    }
//                }
//                unset($service_record['lang_data']);
//            }
//        }
//        $record['service']=$service_records;
//        //处理协议-列表里面不处理协议-详情再处理-和前端商量好再调一次协议接口。详情里也就不用处理了
//        $lang_record=$this->doLangData($record['lang_data']);
//        //合并数组 - 在模型中配置属性，如 image 使用 cdnurl,是在最终的结果时处理的。所以这里直接覆盖 image 就行，不用提前在处理数据
//        if(!empty($lang_record)){
//            foreach ($lang_record as $lk=>$lv){
//                $record[$lk]=$lv;
//            }
//        }
//        unset($record['lang_data']);
        $this->success('操作成功',$record);
    }

}
